//
//  WmcEditBrandVC+Action.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/6/17.
//

import UIKit

extension WmcEditBrandVC {
    /// 品牌库选择品牌图
    func didSelectBrandLibrary(_ brand: WmcWatermarkOptionModel.Brand) {
        let vm = WmcBrandLibraryVM()
        vm.didSelectLogoBlock = { [weak self] model in
            guard let weakSelf = self else { return }
            weakSelf.viewModel.changeImage(.init(url: model.imgUrl))
        }
        WmcNavigatorManager.push(.brandLibrary, parms: vm)
    }

    /// 相册选择品牌图
    func didSelectBrandAlbumSelect(_ brand: WmcWatermarkOptionModel.Brand) {
        let photoAlbumStatus = WmcAuthorizedManager.photoAlbum()
            if photoAlbumStatus.info.isAvailable {
                WmcPhotoSelectManager.photoSelect(vc: self, maxCount: 1, selectBlock: { [weak self] list in
                    guard let weakSelf = self,
                          let image = list.first else { return }
                    weakSelf.viewModel.changeImage(.init(uiImage: image))
                }, allowTakePicture: false)
            } else {
                WmcAuthorizedManager.handleAlbumAuthorizationNotAvailable()
            }
    }

    /// 品牌图裁剪
    func didSelectBrandTailor(_ optionBrand: WmcWatermarkOptionModel, brand: WmcWatermarkOptionModel.Brand) {
        let vm = WmcImageTailorVM()
        vm.title = "裁剪"
        if let localImage = optionBrand.brand.localImage {
            vm.image = localImage
        } else {
            vm.image = .init(url: optionBrand.content)
        }
        vm.croppingImageBlock = { [weak self] image in
            guard let weakSelf = self else { return }
            weakSelf.viewModel.changeImage(.init(uiImage: image))
        }
        WmcNavigatorManager.push(.imageTailor, parms: vm)
    }

    /// 修改品牌图位置
    func didSelectBrandChangeLocation(_ brand: WmcWatermarkOptionModel.Brand) {
        let changeLocationView = WmcEditBrandChangeLocationView()
        changeLocationView.setLocationType(viewModel.locationType())
        let presentVC = WmcPresentVC(config: .init(style: .sheet(), contentView: changeLocationView, contentLayoutBlock: { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(Wmc_safeAreaBottom + 300)
        }))
        presentVC.show()
        // 修改了位置
        changeLocationView.locationTypeChangeBlock = { [weak self] type in
            guard let weakSelf = self else { return }
            weakSelf.viewModel.changeLocationType(type)
            weakSelf.reloadToolViewItem() // 刷新工具栏
        }

        changeLocationView.titleView.leftBtn.yp.addEvent { [weak presentVC] _ in
            guard let weakPresentVC = presentVC else { return }
            weakPresentVC.dismissVC()
        }
    }

    /// 品牌图去底色
    func didSelectBrandRemoveBackgroundColor(_ brand: WmcWatermarkOptionModel.Brand) {}

    /// 修改品牌图透明度
    func didSelectBrandAlpha(_ brand: WmcWatermarkOptionModel.Brand) {
        let alphaView = WmcSliderAlertView(values: [
            .init(title: "0%", progress: 0),
            .init(title: "50%", progress: 0.5),
            .init(title: "100%", progress: 1),
        ], isScrollToStep: false)
        alphaView.title = "调透明度"
        alphaView.submitBtn.isHidden = true
        let alpha = brand.alpha
        alphaView.setProgress(CGFloat(alpha) / 100)
        let presentVC = WmcPresentVC(config: .init(style: .sheet(), contentView: alphaView, contentLayoutBlock: { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(Wmc_safeAreaBottom + 250)
        }))
        presentVC.show()

        alphaView.progressChangeBlock = { [weak self] value in
            guard let weakSelf = self else { return }
            weakSelf.viewModel.changeAlpha(value)
        }

        alphaView.titleView.leftBtn.yp.addEvent { [weak presentVC] _ in
            guard let weakPresentVC = presentVC else { return }
            weakPresentVC.dismissVC()
        }
    }

    /// 修改品牌图大小
    func didSelectBrandScale(_ brand: WmcWatermarkOptionModel.Brand) {
        let sizeView = WmcSliderAlertView(values: [
            .init(title: "小", progress: 0),
            .init(title: "推荐", progress: 0.2),
            .init(title: "大", progress: 1),
        ], isScrollToStep: false)
        sizeView.title = "调大小"
        sizeView.submitBtn.isHidden = true
        sizeView.progressLabel.isHidden = true
        let size = brand.size
        sizeView.setProgress(CGFloat(size) / 100)
        let presentVC = WmcPresentVC(config: .init(style: .sheet(), contentView: sizeView, contentLayoutBlock: { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(Wmc_safeAreaBottom + 250)
        }))
        presentVC.show()

        sizeView.progressChangeBlock = { [weak self] value in
            guard let weakSelf = self else { return }
            weakSelf.viewModel.changeSize(value)
        }

        sizeView.titleView.leftBtn.yp.addEvent { [weak presentVC] _ in
            guard let weakPresentVC = presentVC else { return }
            weakPresentVC.dismissVC()
        }
    }
}

extension WmcEditBrandVC {
    /// 刷新工具栏
    func reloadToolViewItem() {
        var toolList: [WmcEditBrandToolCell.ItemType] = []
        toolList = [
            .brandLibrary,
            .albumSelect,
            .tailor,
            .changeLocation,
        ]
        if viewModel.watermarkModel?.templateId == 1,
           viewModel.watermarkModel?.optionBrand()?.brand.locationType == .followWatermark
        {
        } else {
            toolList.append(.scale)
        }
        toolList.append(.alpha)
        toolView.toolList = toolList
    }

    /// 开启品牌图开关
    func openBrandSwitch() {
        viewModel.watermarkModel?.optionBrand()?.isOpen = true
    }
}
